package com.znyj.cloud.chain.tcphandler;

import com.znyj.cloud.device.constant.SysConstant;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by gaoliwei on 15-1-13.
 */
@Component
@ChannelHandler.Sharable
public class TcpServerInitializer extends ChannelInitializer<SocketChannel> {
    @Resource
    private PackageDispatcherHandler packageDispatcherHandler;

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        System.out.println("TCP Server init channel");
        ChannelPipeline p = ch.pipeline();

        p.addFirst("idleStateHandler", new IdleStateHandler(SysConstant.TIMEOUT_CONNECT, 0, 0));
        p.addAfter("idleStateHandler", "idleEventHandler", new ClientIdleTimeoutHandler());
        p.addLast(new ParseDecodeHandler());
        p.addLast(packageDispatcherHandler);
    }
}
